package;

import nme.display.MovieClip;
import nme.display.SimpleButton;
import nme.events.MouseEvent;
import nme.events.Event;
import nme.display.Bitmap;
import nme.ui.Keyboard;
import nme.ui.Mouse;
import nme.Lib;
import nme.events.KeyboardEvent;

/**
 * @author jonathanmoriarty
 */
class MenuScreen extends MovieClip, implements Screen
{
	private var _currTime:Int;
	private var _prevTime:Int;
	private var _deltaTime:Int;
	
	// SFXR for mac sound generator
	// TODO: Add an animated background.
	//private var _background:Bitmap;
	private var _title:Bitmap;
	inline private static var TITLE_Y:Int = 10;
	
	private var _arcade:Bitmap;
	inline private static var ARCADE_Y:Int = 200;
	
	private var _versus:Bitmap;
	inline private static var VERSUS_Y:Int = 250;
	
	private var _options:Bitmap;
	inline private static var OPTIONS_Y:Int = 300;
	
	private var _select:Bitmap;
	
	private var _confirmed:Bool;
	
	public function new() 
	{
		super();
		addEventListener( Event.REMOVED_FROM_STAGE, onRemoved );
		
		_confirmed = false;
		
		_title = Tools.loadBitmap( "title" );
		_title.x = THF.STAGE_WIDTH/2 - _title.width/2;
		_title.y = TITLE_Y;
		addChild(_title);
		
		_arcade = Tools.loadBitmap( "arcade" );
		_arcade.x = THF.STAGE_WIDTH/2 - _arcade.width/2;
		_arcade.y = ARCADE_Y;
		addChild(_arcade);
		
		_versus = Tools.loadBitmap( "versus" );
		_versus.x = THF.STAGE_WIDTH/2 - _versus.width/2;
		_versus.y = VERSUS_Y;
		addChild(_versus);
		
		_options = Tools.loadBitmap( "options" );
		_options.x = THF.STAGE_WIDTH/2 - _options.width/2;
		_options.y = OPTIONS_Y;
		addChild(_options);
		
		_select = Tools.loadBitmap( "select_arrows" );
		_select.x = THF.STAGE_WIDTH/2 - _select.width/2;
		_select.y = ARCADE_Y;
		addChild(_select);
		
		
		/*_makePlayButton();
		_makeCreditsButton();
		_makeQuitButton();*/
		Mouse.hide();
		show();
		_prevTime = Lib.getTimer();
		//Lib.current.stage.addEventListener( Event.ENTER_FRAME, update );
	}
		
	public function onRemoved( event:Event ):Void
	{		
		if ( Lib.current.stage.hasEventListener( Event.ENTER_FRAME ) )
		{
			Lib.current.stage.removeEventListener( Event.ENTER_FRAME, update );
		}
		if ( hasEventListener( Event.REMOVED_FROM_STAGE ) ) 
		{
   			removeEventListener( Event.REMOVED_FROM_STAGE, onRemoved );
		}
	}
	
	public function _onClickPlay( l_event:MouseEvent ):Void
	{
		dispatchEvent( new NavigationEvent( NavigationEvent.SELECT ) );
	}
	
	public function _onClickCredits( l_event:MouseEvent ):Void
	{
		dispatchEvent( new NavigationEvent( NavigationEvent.CREDITS ) );
	}
	
	public function _onClickQuit( l_event:MouseEvent ):Void
	{
		trace("QUIT CODE SHOULD GO HERE");
	}
	
	public function update( l_event:Event ):Void 
	{	
		_currTime = Lib.getTimer();
		_deltaTime = _currTime - _prevTime;
		_prevTime = _currTime;
		
		if ( !_confirmed )
		{
			if ( InputManager.isKeyPressed( InputManager.UP_KEY ) && _select.y > ARCADE_Y )
			{
				_select.y -= 50;
			}
			
			if ( InputManager.isKeyPressed( InputManager.DOWN_KEY ) && _select.y < OPTIONS_Y )
			{
				_select.y += 50;
			}
			
			if ( InputManager.isKeyPressed( InputManager.ENTER_KEY ) )
			{
				_confirmed = true;
				
				var l_confirmedOption:Int = getConfirmedOption();
				
				if ( l_confirmedOption == 0 )
				{
					dispatchEvent( new NavigationEvent( NavigationEvent.SELECT ) );
				}
				else if ( l_confirmedOption == 1 )
				{
					//dispatchEvent( new NavigationEvent( NavigationEvent.SELECT ) );
				}
				else if ( l_confirmedOption == 2 )
				{
					//dispatchEvent( new NavigationEvent( NavigationEvent.SELECT ) );
				}
				
			}
		}
	}
	
	public function getConfirmedOption():Int
	{
		if ( _select.y == ARCADE_Y )
		{
			return 0;
		}
		else if ( _select.y == VERSUS_Y )
		{
			return 1;
		}
		else
		{
			return 2;
		}
	}
	
	public function show():Void
	{
		this.visible = true;
		//_playButton.addEventListener( MouseEvent.CLICK, _onClickPlay );
		//_creditsButton.addEventListener( MouseEvent.CLICK, _onClickCredits );
		//_quitButton.addEventListener( MouseEvent.CLICK, _onClickQuit );
	}
	
	public function hide():Void
	{
		this.visible = false;
		/*if ( _playButton.hasEventListener( MouseEvent.CLICK ) )
		{
			_playButton.removeEventListener( MouseEvent.CLICK, _onClickPlay );
		}
		if ( _creditsButton.hasEventListener( MouseEvent.CLICK ) )
		{
			_creditsButton.removeEventListener( MouseEvent.CLICK, _onClickCredits );
		}
		if ( _quitButton.hasEventListener( MouseEvent.CLICK ) )
		{
			_quitButton.removeEventListener( MouseEvent.CLICK, _onClickQuit );
		}*/
	}
	
	public function reset():Void
	{
		// nothing needed here at the moment.
	}
	
	public function destroy():Void
	{
		hide();
		/*removeChild( _playButton );
		_playButton = null;
		removeChild( _creditsButton );
		_creditsButton = null;
		removeChild( _quitButton );
		_quitButton = null;*/
	}

}